package com.rjwh.dingdong.client.activity.guide;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.activity.community.CommunityPostDetailActivity;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.util.LogUtil;
import com.rjwh.dingdong.client.util.UrlUtil;
import com.rjwh.dingdong.client.view.HackyViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * @author Kay
 * @ClassName GuideActivity
 * @Description 引导页
 * @date 2016-11-22
 * @history 2016-11-22 author: description:
 */

public class GuideActivity extends BaseActivity {
    @BindView(R.id.guide_viewpager)
    HackyViewPager guideViewpager;
    List<View> views;
    private Context ctx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide_layout);
        ctx = GuideActivity.this;
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        views = new ArrayList<>();
        views.add(getLayoutInflater().inflate(R.layout.viewpager_page1, null));
        views.add(getLayoutInflater().inflate(R.layout.viewpager_page2, null));
        views.add(getLayoutInflater().inflate(R.layout.viewpager_page3, null));
        GuidePagerAdapter guidePagerAdapter = new GuidePagerAdapter(views);
        guideViewpager.setAdapter(guidePagerAdapter);
    }

    private class GuidePagerAdapter extends PagerAdapter {

        //界面列表
        private List<View> views;

        GuidePagerAdapter(List<View> views) {
            this.views = views;
        }

        //销毁arg1位置的界面
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        //获得当前界面数
        @Override
        public int getCount() {
            if (views != null) {
                return views.size();
            }

            return 0;
        }

        //初始化arg1位置的界面
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(views.get(position));
            if (position == views.size() - 1) {
                //最后一页时
                Button btn = (Button) views.get(position).findViewById(R.id.guide_viewpager_button);
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        toMainActivty();
                    }
                });
            }
            return views.get(position);
        }

        //判断是否由对象生成界面
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return (arg0 == arg1);
        }

    }

    private void toMainActivty() {
        if (!MyApplication.spUtil.getBoolPreferenceByParamName(ctx, LocalConstant.SP_IS_LOGINED)) {
            MyApplication.spUtil.setStrPreference(MyApplication.app, LocalConstant.SP_BABYID, "0");
            MyApplication.spUtil.setStrPreference(MyApplication.app, LocalConstant.SP_USERID, "0");
        }
        Intent intentMain = new Intent(ctx, MainActivity.class);
        Uri uri = getIntent().getData();
        //判断程序启动是否传递了参数
        if (getIntent().getBundleExtra(LocalConstant.APP_EXTRA_BUNDLE) != null) {
            intentMain.putExtra(LocalConstant.APP_EXTRA_BUNDLE,
                    getIntent().getBundleExtra(LocalConstant.APP_EXTRA_BUNDLE));
        }

        if (uri != null) {
            Intent intentToActivty = null;
            String forwordType = uri.getQueryParameter("subtype");
            LogUtil.e("SplashActivity", "uri: " + uri.toString());

            if (forwordType.equals(LocalConstant.SCHEME_FORWORD_TYPE_COMMUNITY_DETAIL)) {
                intentToActivty = new Intent(ctx, CommunityPostDetailActivity.class);
                String filepath = uri.getQueryParameter("filepath");
                String topicid = uri.getQueryParameter("topicid");
                String webUrl = UrlUtil.getWebUrl(ctx, filepath);
                intentToActivty.putExtra("url", webUrl);
                intentToActivty.putExtra("topicid", topicid);
            }

            if (intentToActivty == null) {
                LogUtil.e("HTML", "SplashActivity intentToActivty==NULL");
                startActivity(intentMain);
            } else {
                LogUtil.e("HTML", "SplashActivity intentArgs");
                Intent[] intentArgs = {intentMain, intentToActivty};
                startActivities(intentArgs);
            }
        } else {
            LogUtil.e("HTML", "SplashActivity intentMain");
            startActivity(intentMain);
        }
        MyApplication.spUtil.setBoolPreference(MyApplication.getInstance(), LocalConstant.SP_IS_GUIDE_SHOWN, true);
        GuideActivity.this.finish();
    }
}